package com.haha.Actions;

import com.haha.Beans.Media;
import com.haha.Common.VideoFilter;
import com.haha.Common.UUIDGenerator;
import com.haha.Services.impls.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import static com.haha.Common.FfmpegUtil.*;
import static com.haha.Common.FfmpegUtil.checkVideoFile;


@RestController
@RequestMapping("/media")
public class MediaAction {
    @Autowired
    private MediaService mediaService;

    @Value("${upload.path}")
    private String uploadPath;
    @PostMapping("/upload")
    public ResponseEntity<String> uploadVideo(@RequestParam("file") MultipartFile file){
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body("文件为空，上传失败");
        }

        try {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            String fileType = file.getContentType();

            // video/mp4, image/png, audio/mpeg
            if (fileName != null && fileType != null){
                // 创建文件保存路径
                String videoSubFile = uploadPath + fileName.split("\\.")[0];
                if (checkVideoFile(videoSubFile)) {
                    File targetFile = new File(videoSubFile + File.separator + fileName);
                    // 保存文件
                    file.transferTo(targetFile);

                    Media uploadMedia = new Media();
                    // 生成该记录的唯一ID
                    String videoUUID = UUIDGenerator.GenerateUUID();
                    uploadMedia.setMediaId(videoUUID);
                    uploadMedia.setMediaTitle(fileName);
                    uploadMedia.setMediaType(fileType);
                    uploadMedia.setMediaUploadDate(LocalDateTime.now().toString());
                    uploadMedia.setMediaPath(videoSubFile + "/" + fileName);

                    // 获取音视频的时长
                    uploadMedia.setMediaDuration(getVideoDurationByCv(videoSubFile+"/"+uploadMedia.getMediaTitle()));
                    uploadMedia.setMediaFirstFrame(getFirstFrameByCv(videoSubFile, uploadMedia.getMediaTitle()));
                    System.out.println(uploadMedia);
                    this.mediaService.insertMedia(uploadMedia);
                    return ResponseEntity.ok("文件上传成功");
                }
            }
            return ResponseEntity.status(500).body("文件处理失败");
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("文件上传失败");
        }
    }

    @GetMapping("/getMedia/{id}")
    public ResponseEntity<Media> getMeida(@PathVariable Integer id){
        Media media = this.mediaService.selectMediaById(id);
        if (media != null){
            return ResponseEntity.ok(media);
        }
        return ResponseEntity.notFound().build();
    }

    @RequestMapping("/Medias")
    List<Media> getBooks(){
        return this.mediaService.getAllMedia();
    }


    public void mergeVideo(){
        String[] videoes = new String[]{"E:\\Coding\\IDEA\\资源\\VideoSavingPath\\cut_40s\\cut_40s.mp4","E:\\Coding\\IDEA\\资源\\VideoSavingPath\\digitalhuman\\digitalhuman.mp4"};
        VideoFilter filter = new VideoFilter(){

        };


    }
}
